<template>
  <div style="width: 100%;height: 100%;">
    <div class="header">
      <div class="logo">
        <img src="../../assets/img/company-logo.png" alt />
        <span>政务百科</span>
      </div>
      <div style="flex:1;">
        <div class="user-info right">
          <el-dropdown trigger="click" @command="handleCommand">
            <span class="el-dropdown-link">
              <i class="el-icon-user-solid" style="color: #DDDDDD;"></i>
              {{username}}
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="loginout">退出</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
        <el-menu
          :default-active="ac"
          mode="horizontal"
          router
          class="el-menu-top right"
          text-color="#333333"
          active-text-color="#1492FF"
        >
          <el-menu-item
            v-for="(item,index) in muenlist"
            :key="index"
            :index="item.index"
            @click="dianji(item)"
          >
            <i id="icon" :class="item.icon"></i>
            {{item.name}}
          </el-menu-item>
        </el-menu>
      </div>
    </div>
    <el-menu
      :default-active="active"
      class="sidebar"
      router
      background-color="#fff"
      text-color="#000"
      active-text-color="#1492FF"
      v-for="(r,index) in muenlist"
      :key="index"
      v-if="xz == r.name"
    >
      <template v-for="item in r.childMenuList">
        <template v-if="item.childMenuList">
          <el-submenu :index="item.index" class="menu_lanmu">
            <template slot="title">
              <i id="icon" :class="item.icon"></i>
              <span>{{ item.name }}</span>
            </template>
            <el-menu-item
              style="padding-left:50px; text-align: center;"
              v-for="(subItem,i) in item.childMenuList"
              :key="i"
              :index="subItem.index"
            >{{ subItem.name }}</el-menu-item>
          </el-submenu>
        </template>
        <template v-else>
          <el-menu-item :index="item.index">
            <i id="icon" :class="item.icon"></i>
            {{ item.name }}
          </el-menu-item>
        </template>
      </template>
    </el-menu>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        aa: '', // 路由后面第一位，例如clopedia
        active: '',// 完整的路由路径，如/clopedia/matterIndex
        muenlist: [],// 所有的menu（包括系统管理和政务百科）
        ac: '', // 完整的路由路径，如/clopedia/matterIndex
        xz: '',// 标题，如政务百科
        name: 'admin'
      }
    },
    computed: {
      username() {
        let username = JSON.parse(localStorage.getItem("loginToken")).userName;
        return username ? username : this.name;
      }
    },

    watch: {
      $route(to, from) {
        const arr = to.path.split('/')
        console.log(arr)
        if(arr[1] === 'clopedia'){
          this.ac = '/clopedia/matterIndex'
          this.active = '/clopedia/matterIndex'
          this.aa = 'clopedia'
          // console.log(this.ac)
          // console.log(this.active)
          // console.log(this.aa)
        }
        this.xuanzhong()
      }
    },
    mounted() {
      if (window.location.href.indexOf('/login') > -1 || window.location.hash == '#/' || window.location.href.indexOf(
          '/hello') > -1)
        document.querySelector('html').setAttribute('style', 'font-size:' + deviceWidth / 19.2 + 'px')
      else
        document.querySelector('html').removeAttribute("style");
      this.menuList();
      this.xuanzhong()
      if(this.$route.path.split('/')[1] === 'clopedia'){
        this.ac = '/clopedia/matterIndex' // 系统管理、政务百科tab页
        this.aa = 'clopedia'
        this.dianji(this.muenlist[1])
      }
      // console.log(this.$route.path.split('/'))
      // console.log(this.muenlist)
    },
    methods: {
      handleCommand(command) {
        if (command == 'loginout') {
          localStorage.removeItem('loginToken');
          localStorage.removeItem('authorization');
          this.$router.push('/login');
        }
      },
      xuanzhong() {
        //return this.$route.path.replace('/','');
        this.aa = this.$route.name ? this.$route.name : this.$route.path.replace('/', '');
        if (this.aa == "shixiang11" || this.aa == "shixiang12" || this.aa == "shixiang15") {
          this.aa = localStorage.getItem('router');
        }

        this.aa = '/' + this.aa;
        for (var i in this.muenlist) {
          var menu = this.muenlist[i].childMenuList
          //console.log(menu)
          for (var a in menu) {
            var menus = menu[a].childMenuList;

            if (menus != undefined) {
              for (var j in menus) {
                if (menus[j].index == this.aa || this.aa.indexOf('/' + menu[a].index) >= 0) {
                  this.active = menus[j].index;
                  this.xz = this.muenlist[i].name
                  this.ac = this.muenlist[i].index
                }
              }
            } else {
              if (menu[a].index == this.aa || this.aa.indexOf(menu[a].index) >= 0) {
                this.active = menu[a].index;
                this.xz = this.muenlist[i].name
                this.ac = this.muenlist[i].index
              }
            }
          }

        }
      },
      dianji(r) {
        if (r.index == this.ac)
        // window.location.reload()
        this.active = r.index;
        this.xz = r.name
        this.ac = r.index;
      },
      //菜单
      menuList() {
        // 从登陆信息中获取
        var arr = JSON.parse(localStorage.getItem("loginToken"));
        this.muenlist = localStorage.getItem("menuList");
        if (!this.active && !this.aa) {
          this.dianji(this.muenlist[0])
        }

      },
    }
  }
</script>

<style scoped>
.el-menu {
  overflow: hidden;
}

.el-menu-top.el-menu--horizontal.el-menu li {
  height: 80px;
  font-size: 18px;
  line-height: 80px;
}

.el-menu-top.el-menu--horizontal.el-menu .el-menu-item:hover {
  background: #fff;
  color: rgb(20, 146, 255) !important;
  border-bottom-color: rgb(20, 146, 255) !important;
  border-bottom-width: 4px;
}

.el-menu-top.el-menu--horizontal.el-menu .el-menu-item.is-active {
  background: #fff;
  border-bottom-width: 4px;
}

.el-menu-top {
  display: inline-block;
  width: auto;
  background: none;
  border-bottom: none;
}

.sidebar {
  display: block;
  position: absolute;
  width: 12.19512%;
  left: 0;
  top: 80px;
  bottom: 0;
  background: #2e363f;
}

ul.sidebar {
  border: none;
  height: 100%;
  width: 12.19512% !important;
  padding: 0;
  margin: 0;
}

ul.sidebar > li.el-menu-item {
  text-align: center;
}

.sidebar.el-menu .el-menu-item.is-active,
.sidebar.el-menu li:hover {
  background: rgba(20, 146, 255, 0.04) !important;
}

.sidebar.el-menu .el-menu-item.is-active:after,
.sidebar.el-menu li:hover:after {
  content: "";
  display: block;
  width: 4px;
  height: 56px;
  background: #1492ff;
  position: absolute;
  left: 0;
  top: 0;
}

.header {
  position: relative;
  display: flex;
  box-sizing: border-box;
  width: 100%;
  height: 80px;
  font-size: 22px;
  line-height: 80px;
  color: #fff;
  background: #ffffff;
  text-align: center;
}

.header .logo {
  float: left;
  padding-left: 20px;
  height: 80px;
}

.header .logo img {
  height: 60px;
  float: left;
  padding-top: 10px;
}

.header .logo span {
  font-size: 26px;
  font-family: PingFangSC;
  font-weight: 600;
  color: rgba(51, 51, 51, 1);
  line-height: 42px;
  padding-left: 10px;
}

.user-info {
  float: right;
  padding-right: 50px;
  font-size: 16px;
  color: #fff;
}

.user-info .el-dropdown-link {
  position: relative;
  display: inline-block;
  padding-left: 50px;
  line-height: 80px;
  color: #333333;
  cursor: pointer;
}

.user-info .user-logo {
  position: absolute;
  left: 0;
  top: 15px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
}

.el-dropdown-menu__item {
  text-align: center;
}

.el-popper[x-placement^="bottom"] {
  margin-top: 0;
}
</style>
